let _target = undefined;

const D = function (el) {
    if(el instanceof Element) {
        _target = el;
        return D;
    }
    if(el instanceof DocumentFragment) {
        _target = el;
        return D;
    }
}

D.AddClass = function (name) {
    _target.classList.add(name);
    return D;
}
D.RemoveClass = function (name) {
    _target.classList.remove(name);
    return D;
}
D.ToggleClass = function (name, force) {
    _target.classList.toggle(name, force);
    return D;
}
D.ReplaceClass = function (oldName, newName) {
    _target.classList.replace(oldName, newName);
    return D;
}
D.HasClass = function (name) {
    return _target.classList.contains(name);
}

D.AddStyle = function (key, value) {
    _target.style.setProperty(key, value);
    return D;
}
D.GetStyle = function (key) {
    return _target.style.getPropertyValue(key);
}
D.RemoveStyle = function (key) {
    _target.style.removeProperty(key);
    return D;
}
D.ToggleStyle = function (key, value) {
    if(_target.style.getPropertyValue(key) === '') {
        _target.style.setProperty(key, value);
    } else {
        _target.style.removeProperty(key);
    }
    return D;
}
D.AddAttribute = function (key, value) {
    _target.setAttribute(key, value);
    return D;
}
D.RemoveAttribute = function (key) {
    _target.removeAttribute(key);
    return D;
}
D.HasAttribute = function (name) {
    return _target.hasAttribute(name);
}
D.GetAttribute = function (name) {
    return _target.getAttribute(name);
}

D.CacheRestoreStyle = function (key, value) {
    const _value = _target.style.getPropertyValue(key);
    _target.style.setProperty(key, value);
    return () => {
        _target.style.setProperty(key, _value);
    }
}
D.StyleSwitcher = function (key, toValue) {
    const _target2 = _target;
    const _fromValue = _target.style.getPropertyValue(key);
    return {
        Do: () => {
            _target2.style.setProperty(key, toValue);
        },
        Restore: () => {
            _target2.style.setProperty(key, _fromValue);
        }
    }
}

D.GetDatasetValue = function (key) {
    return _target.dataset[key];
}

D.QueryElement = function (value) {
    return _target.querySelector(value);
}

D.Qe = function (value) {
    return _target.querySelector(value);
}
// QueryElement and continue method D;
D.QeC = function (value) {
    return D(D.Qe(value));
}

D.Append = function (value) {
    _target.append(value);
    return D;
}
D.Prepend = function (value) {
    _target.prepend(value);
    return D;
}
D.Before = function (value) {
    _target.before(value);
    return D;
}
D.After = function (value) {
    _target.after(value);
    return D;
}
D.Remove = function (value) {
    if(value.parentElement == _target) {
        value.remove();
    }
    return D;
}
D.RemoveSelf = function () {
    _target.remove();
    return D;
}
D.RemoveAllChildren = function () {
    _target.replaceChildren();
    return D;
}

D.SetInnerHTML = function (value) {
    _target.innerHTML = value;
    return D;
}
D.SetInnerText = function (value) {
    _target.innerText = value;
    return D;
}
D.GetNodeName = function () {
    return _target.nodeName;
}
D.HasTarget = function () {
    return !!_target;
}
D.SetParentAsTarget = function () {
    _target = _target.parentElement;
    return D;
}
D.GetID = function () {
    return _target.id;
}
D.SetID = function (value) {
    _target.id = value;
    return D;
}

D.SetInputValue = (value) => {
    const _type = _target.getAttribute('type');
    switch(_type) {
        case 'text':
            _target.value = value;
            break;
        case 'date':
            _target.value = value.toJSON().slice(0, 10);
            break;
        case 'checkbox':
            _target.checked = value;
            break;
        default:
            console.warn(`no such type:${_type}`);
    }
    return D;
}

D.GetInputValue = () => {
    const _type = _target.getAttribute('type');
    switch(_type) {
        case 'text':
        case 'date':
        case 'search':
            return _target.value;
        case 'checkbox':
            return _target.checked;
        default:
            console.warn(`no such type:${_type}`);
            return undefined;
    }
}

Object.freeze(D);
export default D;
